package com.bst.bsj.bops.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bst.bsj.base.controller.WebBaseController;
import com.bst.bsj.bops.web.biz.SeedlingModelParamsService;
import com.bst.bsj.bops.web.biz.SeedlingTypeModelParamsRelationService;
import com.bst.bsj.bops.web.biz.SeedlingTypeService;
import com.bst.bsj.common.dal.dataobject.Merchant;
import com.bst.bsj.common.dal.dataobject.SeedlingModelParams;
import com.bst.bsj.common.dal.dataobject.SeedlingType;
import com.bst.bsj.common.dal.dataobject.SeedlingTypeModelParamsRelation;
import com.homewaimai.common.lang.Paginator;
import com.homewaimai.common.lang.Result;
import com.homewaimai.common.lang.StringUtil;

@Scope("prototype")
@Controller
@RequestMapping(value = "/bops/sys/seedlingType")
public class SeedlingTypeController extends WebBaseController {

  @Autowired
  private SeedlingTypeService seedlingTypeService;

  @Autowired
  private SeedlingModelParamsService seedlingModelParamsService;

  @Autowired
  private SeedlingTypeModelParamsRelationService modelParamsRelationService;

  /**
   * 
  * @Title: auditList
  * @Description: TODO(这里用一句话描述这个方法的作用)
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月14日 上午11:41:37
  * @modificator :tonglu
  * @modify date :2016年5月14日 上午11:41:37
   */
  @SuppressWarnings("unchecked")
  @RequestMapping(value = "/seedlingTypeList")
  public ModelAndView auditList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    ModelAndView modelAndView = getBopsModelAndView("goods/classified");
    String name = (String) params.get("name");//名称
    if (name != null) {
      name = name.trim();
    }
    String pageNums = (String) params.get("pageNum");
    Integer pageNum = 0;
    if (StringUtil.isNotBlank(name)) {
      paramsMap.put("name", name);
    }
    if (StringUtil.isNumeric(pageNums)) {
      pageNum = Integer.valueOf(pageNums);
    }
    if (pageNum == null || pageNum == 0) {
      pageNum = 1;
    }
    paramsMap.put("page", pageNum);
    paramsMap.put("length", new Long(10));
    paramsMap.put("offset", new Long((pageNum - 1) * 10));
    Result result = seedlingTypeService.query(paramsMap);
    // 如果请求成功
    if (result.isSuccess()) {
      // 得到分页器
      Paginator paginator = (Paginator) result.getModels().get("paginator");
      // 得到数据
      List<SeedlingType> seedlingTypes = (List<SeedlingType>) result.getModels().get("seedlingTypes");
      modelAndView.addObject("seedlingTypes", seedlingTypes);
      modelAndView.addObject("paginator", paginator);
    }
    modelAndView.addObject("params", params);
    return modelAndView;
  }

  /**
   * 
  * @Title: edit
  * @Description: TODO(编辑)
  * @param req
  * @param resp
  * @param id
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月13日 下午7:34:50
  * @modificator :tonglu
  * @modify date :2016年5月13日 下午7:34:50
   */
  @RequestMapping(value = "/edit")
  public ModelAndView edit(HttpServletRequest req, HttpServletResponse resp, String id) throws Exception {
    ModelAndView modelAndView = new ModelAndView();
    SeedlingType seedlingType = seedlingTypeService.getSeedlingType(Long.valueOf(id));
    modelAndView.addObject("seedlingType", seedlingType);
    return modelAndView;
  }

  @RequestMapping(value = "/save")
  @ResponseBody
  public String save(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    SeedlingType seedlingType = new SeedlingType();
    String id = (String) params.get("id");
    Long modelParamsRelationId = 0L;
    if (id != null) {
      seedlingType = seedlingTypeService.getSeedlingType(Long.valueOf(id));
    }
    String name = (String) params.get("name");
    String unit = (String) params.get("unit");
    String modelParamsNumbers = (String) params.get("type");
    seedlingType.setName(name);
    seedlingType.setUnit(unit);
    String[] strs = modelParamsNumbers.split(",");
    if (id != null) {
      seedlingTypeService.update(seedlingType);
      modelParamsRelationId = Long.valueOf(id);
    } else {
      modelParamsRelationId = seedlingTypeService.insert(seedlingType);
    }
    modelParamsRelationService.delete(modelParamsRelationId);
    if (!"".equals(modelParamsNumbers)) {
      for (String string : strs) {
        SeedlingTypeModelParamsRelation modelParamsRelation = new SeedlingTypeModelParamsRelation();
        modelParamsRelation.setSeedlingTypeNumber(modelParamsRelationId + "");
        modelParamsRelation.setModelParamsNumber(string);
        modelParamsRelationService.insert(modelParamsRelation);
      }
    }

    return "success";
  }

  /**
   * 
  * @Title: getTypes
  * @Description: TODO(获取所有的分类)
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * List<SeedlingType>
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月17日 下午4:26:10
  * @modificator :tonglu
  * @modify date :2016年5月17日 下午4:26:10
   */
  @RequestMapping(value = "/getTypes")
  @ResponseBody
  public List<SeedlingType> getTypes(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    List<SeedlingType> list = seedlingTypeService.getAll();
    return list;
  }

  @RequestMapping(value = "/delete")
  public void delete(HttpServletRequest req, HttpServletResponse resp, String id) throws Exception {
    seedlingTypeService.delete(Long.valueOf(id));
    modelParamsRelationService.delete(Long.valueOf(id));
  }

  /**
   * 
  * @Title: getCountByName
  * @Description: TODO(判断类型名称是否重复)
  * @param req
  * @param resp
  * @param name
  * @param id
  * @return
  * @throws Exception
  * Boolean
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月21日 下午7:27:49
  * @modificator :tonglu
  * @modify date :2016年5月21日 下午7:27:49
   */
  @RequestMapping(value = "/getCountByName")
  @ResponseBody
  public Boolean getCountByName(HttpServletRequest req, HttpServletResponse resp, String name, Long id)
      throws Exception {
    if (StringUtil.isNotBlank(name)) {
      SeedlingType seedlingType = seedlingTypeService.getCountByName(name.trim());
      if (seedlingType == null) {
        return true;
      }else{
         if(id!=null){
           Long mid=seedlingType.getId();
            if(mid.equals(id)){
              return true;
            }
         }
      }
    }
    return false;
  }
}
